/**
 * 将字符串转换为驼峰命名法。
 * @param {string} str 输入字符串，例如 "hello-world", "user_name", "first name"
 * @returns {string} 转换后的字符串，例如 "helloWorld", "userName", "firstName"
 */
export function camelCase(str: string): string {
    if (str.length === 0) {
        return '';
    }
    return str
        .replace(/[^a-zA-Z0-9]/g, ' ')
        .toLowerCase()
        .split(' ')
        .map((word, index) => {
            if (index === 0) {
                return word;
            }
            return word.charAt(0).toUpperCase() + word.slice(1);
        })
        .join('');
}

/**
 * 获取token
 */
export const getToken = (): string => sessionStorage.getItem('token')!

/**
 * 获取API地址
 */
export const getBaseUrl = (): string => import.meta.env.VITE_API_BASE_URL

/**
 * 格式化时间
 * @param time
 * @param format
 */
export const formatterTime = (time: number, format: string = 'YYYY-MM-DD HH:mm:ss'): string => {
    const date = new Date(time);
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const second = date.getSeconds();

    return format
        .replace(/MM/g, String(month).padStart(2, '0'))
        .replace(/DD/g, String(day).padStart(2, '0'))
        .replace(/HH/g, String(hour).padStart(2, '0'))
        .replace(/mm/g, String(minute).padStart(2, '0'))
        .replace(/ss/g, String(second).padStart(2, '0'))
        .replace(/YYYY/g, String(year))
}


export const download = (blob: Blob, filename: string): void => {
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = filename;
    a.click();
    window.URL.revokeObjectURL(url);
    a.remove();
}